/*
 * Copyright (c) 2017, dhl and/or its affiliates. All rights reserved.
 * dhl PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 *
 */
package com.xd.cloud.web;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.xd.cloud.common.Constants;
import com.xd.cloud.common.bean.MessageBean;
import com.xd.cloud.common.bean.VehicleInfoBean;
import com.xd.cloud.dao.Page;
import com.xd.cloud.entity.vehicle.VehicleInfo;
import com.xd.cloud.service.vehicle.VehicleInfoService;

/**
 * VehicleInfoController
 * 
 * @author wangtao
 * @description 车辆信息控制层
 * @since V1.0
 * 
 */
@Controller
@RequestMapping("/vehicleinfo")
public class VehicleInfoController extends BaseController {

	@Autowired
	private VehicleInfoService vehicleInfoService;

	/**
	* 车辆列表
	* @return Object
	*/
	@RequestMapping(value = "/getVehicleList")
	@ResponseBody
	public Object getVehicleList(HttpServletRequest request, HttpServletResponse response) {
	//直接返回前台  
	String draw = request.getParameter("draw");  
	//数据起始位置  
	//String startIndex = request.getParameter("start");  
	//每页显示的条数 
	String num = request.getParameter("page");  
	String pageSize = request.getParameter("limit");   
	int sindex = Integer.parseInt(num);
	int ps = Integer.parseInt(pageSize);
	Page page = vehicleInfoService.getAll(sindex,ps);//roleService.getAllRole(sindex, ps);
	
	Map<Object, Object> info = new HashMap<Object, Object>();  
	
	List<VehicleInfoBean> list = new ArrayList();
	List<Object[]> vehlist = page.getResult();
	if (vehlist != null)
	{
		for (Object[] veh:vehlist)
		{
			//a.id,a.vehid,a.platetype,a.vehcolor,b.id as orgid,b.name as orgname,a.devicecode,a.status
			VehicleInfoBean ob = new VehicleInfoBean();
			ob.setId((Integer)veh[0]);
			ob.setVehid((String)veh[1]);
			ob.setPlatetype((String)veh[2]);
			ob.setVehcolor((String)veh[3]);
			if (veh[4] != null)
			ob.setOrgid((Integer)veh[4]);
			ob.setOrgname((String)veh[5]);
			ob.setDevicecode((String)veh[6]);
			//暂时两种状态，后续业务跟上后这儿再修改
			ob.setStatus((Integer)veh[7] == 0?Constants.USER_OK:Constants.DEV_STATUS2);
			list.add(ob);
		}
	}
	
	info.put("pageData", list);  
	info.put("total", page.getTotalCount()); 
	info.put("draw", draw);  
	
	return info;
	}

	/**
	* 返回id返回车辆信息
	* @return Object
	*/
	@RequestMapping(value = "/getVehinfo")
	@ResponseBody
	public Object getVehinfo(HttpServletRequest request, int id) {

		VehicleInfo info = vehicleInfoService.get(id);
		return new MessageBean(true,"",info);
	}
	
	/**
	 * 删除车辆
	 * @return Object
	 */
	@RequestMapping(value = "/delVeh")
	@ResponseBody
	public Object delVeh(HttpServletRequest request,int id) {
		
		boolean flag = vehicleInfoService.remove(id);
		return new MessageBean(flag,flag?Constants.SUCCESS_3:Constants.ERROR_10);
	}
	
	/**
	 * 保存车辆
	 * @return Object
	 */
	@RequestMapping(value = "/saveVeh")
	@ResponseBody
	public Object saveVeh(HttpServletRequest request,VehicleInfo info) {//String vehid,int platetype,String vehcolor,int companyid,int status) {
		info.setCreatetime(new Date());
		boolean flag = vehicleInfoService.saveVehicle(info);
		return new MessageBean(flag,flag?Constants.SUCCESS_1:Constants.ERROR_11);
	}
	
	/**
	 * 编辑车辆
	 * @return Object
	 */
	@RequestMapping(value = "/updateVeh")
	@ResponseBody
	public Object updateVeh(HttpServletRequest request,VehicleInfo info) {
		info.setUpdatetime(new Date());
		boolean flag = vehicleInfoService.update(info);
		return new MessageBean(flag,flag?Constants.SUCCESS_2:Constants.ERROR_5);
	}
	
	/**
	 * 车辆绑定设备
	 * @return Object
	 */
	@RequestMapping(value = "/bindVeh")
	@ResponseBody
	public Object bindVeh(HttpServletRequest request,int id,String devicecode) {
		boolean flag = vehicleInfoService.updateVehicle(id,devicecode);
		return new MessageBean(flag);
	}
}